// 编辑器通用逻辑混入
export default {
    data() {
        return {
            editor: null,
            isModified: false,
            lastSaved: null,
            fileName: '',
            currentLine: 1,
            currentColumn: 1,
            totalLines: 1
        }
    },
    methods: {
    // 初始化编辑器事件监听
        initEditorEvents() {
            if (!this.editor) return
      
            // 监听内容变化
            this.editor.on('change', () => {
                this.isModified = true
                this.updateLineInfo()
            })
      
            // 监听光标位置变化
            this.editor.on('changeSelection', () => {
                this.updateCursorPosition()
            })
      
            // 监听滚动事件
            this.editor.on('changeScrollTop', () => {
                this.updateLineInfo()
            })
        },
    
        // 更新光标位置信息
        updateCursorPosition() {
            if (!this.editor) return
      
            const cursor = this.editor.getCursorPosition()
            this.currentLine = cursor.row + 1
            this.currentColumn = cursor.column + 1
        },
    
        // 更新行信息
        updateLineInfo() {
            if (!this.editor) return
      
            this.totalLines = this.editor.session.getLength()
        },
    
        // 设置编辑器内容
        setEditorContent(content) {
            if (!this.editor) return
      
            this.editor.setValue(content, -1)
            this.isModified = false
            this.updateLineInfo()
            this.updateCursorPosition()
        },
    
        // 获取编辑器内容
        getEditorContent() {
            return this.editor ? this.editor.getValue() : ''
        },
    
        // 插入文本
        insertText(text) {
            if (!this.editor) return
      
            this.editor.insert(text)
            this.editor.focus()
        },
    
        // 撤销操作
        undo() {
            if (!this.editor) return
      
            this.editor.undo()
        },
    
        // 重做操作
        redo() {
            if (!this.editor) return
      
            this.editor.redo()
        },
    
        // 检查是否可以撤销
        canUndo() {
            return this.editor ? this.editor.session.getUndoManager().hasUndo() : false
        },
    
        // 检查是否可以重做
        canRedo() {
            return this.editor ? this.editor.session.getUndoManager().hasRedo() : false
        },
    
        // 查找文本
        find(searchText, options = {}) {
            if (!this.editor) return
      
            this.editor.find(searchText, {
                backwards: options.backwards || false,
                wrap: options.wrap !== false,
                caseSensitive: options.caseSensitive || false,
                wholeWord: options.wholeWord || false,
                regExp: options.regExp || false
            })
        },
    
        // 替换文本
        replace(searchText, replaceText, options = {}) {
            if (!this.editor) return
      
            this.editor.replace(replaceText, options)
        },
    
        // 全部替换
        replaceAll(searchText, replaceText, options = {}) {
            if (!this.editor) return
      
            this.editor.replaceAll(replaceText, options)
        },
    
        // 格式化代码
        formatCode() {
            if (!this.editor) return
      
            // Lua代码格式化逻辑（简单实现）
            const content = this.getEditorContent()
            const formatted = this.formatLuaCode(content)
            this.setEditorContent(formatted)
        },
    
        // 简单的Lua代码格式化
        formatLuaCode(code) {
            const lines = code.split('\n')
            let indentLevel = 0
            const indentSize = 2
      
            return lines.map(line => {
                const trimmed = line.trim()
        
                // 减少缩进的关键字
                if (trimmed.match(/^(end|else|elseif|until)$/)) {
                    indentLevel = Math.max(0, indentLevel - 1)
                }
        
                const formatted = ' '.repeat(indentLevel * indentSize) + trimmed
        
                // 增加缩进的关键字
                if (trimmed.match(/^(function|if|for|while|repeat|do)/) && !trimmed.includes('end')) {
                    indentLevel++
                } else if (trimmed.includes('then') || trimmed.includes('do')) {
                    indentLevel++
                }
        
                return formatted
            }).join('\n')
        },
    
        // 保存文件状态
        markAsSaved() {
            this.isModified = false
            this.lastSaved = new Date()
        },
    
        // 设置文件名
        setFileName(name) {
            this.fileName = name
        }
    }
}